﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;

namespace DomainModel.Concrete
{
    // Implementation of the PersonalReferences section of the database.
    // The middle layer of the DAL.
    class SqlPersonalReferencesRepository : IPersonalReferencesRepository
    {
        private Table<PersonalReference> personalReferencesTable;

        public SqlPersonalReferencesRepository(string connectionString)
        {
            personalReferencesTable = (new DataContext(connectionString)).GetTable<PersonalReference>();
        }

        public IQueryable<PersonalReference> PersonalReferences
        {
            get { return personalReferencesTable; }
        }

        public void SavePersonalReference(PersonalReference personalReference)
        {
            if (personalReference.ReferenceID == 0)
            {
                personalReferencesTable.InsertOnSubmit(personalReference);
            }
            else
            {
                personalReferencesTable.Attach(personalReference);
                personalReferencesTable.Context.Refresh(RefreshMode.KeepCurrentValues, personalReference);
            }

            personalReferencesTable.Context.SubmitChanges();
        }

        public void DeletePersonalReference(PersonalReference personalReference)
        {
            personalReferencesTable.DeleteOnSubmit(personalReference);
            personalReferencesTable.Context.SubmitChanges();
        }
    }
}